<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>主页</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
    <!--    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"-->
    <!--          integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous"/>-->
    <!--    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>-->
    <link rel="stylesheet" th:href="@{/bootstrap.min.css}" />
    <script th:src="@{/vue.js}"></script>
</head>
<body>



<div class="container-fluid" id="app" style="margin-top: 180px;margin-bottom: 100px">

    <div class=" navbar navbar-default navbar-fixed-top">
        <div class="container-fluid" >

            <div class="row">
                <div class="col-md-12 text-center" style="background-color: cornflowerblue; color: white">
                    <a th:href="@{/}" style="color: white"><h3>蜗牛书店</h3></a>
                </div>
            </div>


            <div class="row" style="margin-top: 10px;margin-bottom: 10px">

                <div class="col-md-4 text-center">
                    <button class="btn btn-primary" data-toggle="modal" data-target="#myModal" @click="clearAddForm">新增图书</button>
                </div>

            </div>


        </div>


    </div>

    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">新增图书</h4>
                </div>
                <div class="modal-body">
                    <form>
                        <div class="form-group">
                            <label for="typeId">图书类型</label>
                            <select class="form-control" id="typeId" placeholder="请选择类型" v-model="typeId">
                                <option value="-1">请选择类型</option>
                                <option v-for="bookType in bookTypes" :value="bookType.id" v-text="bookType.name"></option>
                            </select>
                        </div>

                        <div class="form-group">
                            <label for="name">书名</label>
                            <input type="text" v-model="name" class="form-control" id="name" placeholder="请输入书名">
                        </div>

                        <div class="form-group">
                            <label for="provider">出版社</label>
                            <input type="text" v-model="provider" class="form-control" id="provider" placeholder="请输入出版社">
                        </div>

                        <div class="form-group">
                            <label for="author">作者</label>
                            <input type="text" v-model="author" class="form-control" id="author" placeholder="请输入作者名">
                        </div>
                        <div class="form-group">
                            <label for="price">价格</label>
                            <input type="text" v-model="price" class="form-control" id="price" placeholder="请输入价格">
                        </div>
                        <div class="form-group">
                            <label for="detail">描述</label>
                            <input type="text" v-model="detail" class="form-control" id="detail" placeholder="请输入描述">
                        </div>
<!--                        <div class="form-group">-->
<!--                            <input type="file" name="imgsrc" class="form-control">-->
<!--                        </div>-->
                        <div class="form-group">
                            <label for="storecount">库存数量</label>
                            <input type="text" v-model="storecount" class="form-control" id="storecount" placeholder="请输入库存">
                        </div>
                    </form>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" @click.prevent="saveBook">保存</button>
                </div>
            </div>
        </div>
    </div>






    <div class="container">

        <div class="row">
            <div class="col-md-12">
                <p style="background-color: cornflowerblue ; color: white; font-size: 30px">所有图书</p>
            </div>
        </div>

        <div class="row">
            <div class="col-sm-6 col-md-4" v-for="book in books">
                <div class="thumbnail">
                    <img :src="book.imgsrc" alt="..." class="col-sm-6 col-md-6">
                    <div class="caption text-left">
                        <p>书名:<span v-text="book.name">name</span></p>
                        <p>价格:<span v-text="book.price">price</span></p>
                        <p>
                            <a class="btn btn-primary" :href="'/book/single?bookId='+book.id">详情</a> &nbsp;&nbsp;&nbsp;&nbsp;
                        </p>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12 col-md-offset-5 col-lg-offset-4col-xl-offset-5" >
                <ul class="pagination justify-content-center">
                    <li><a href="javascript:void(0)" @click.prevent="go(currentPage-1)">&laquo;</a></li>
                    <li><a href="javascript:void(0)" v-for="n in totalPages" v-text="n" @click.prevent="go(n)">n</a></li>
                    <li><a href="javascript:void(0)" @click.prevent="go(currentPage+1)" >&raquo;</a></li>
                </ul>
            </div>
        </div>

    </div>


</div>

<script>

    console.log(".........index...........")

    new Vue({
        el: "#app"
        ,
        data: {
            books:[],

            currentPage:1,
            pageSize:6,
            total:0,
            totalPages:0,

            bookTypes: [],
            typeId:-1,
            name:'',
            provider:'',
            author:'',
            price:0.00,
            detail:'',
            // imgsrc:'',
            storecount:0,


        }
        ,
        methods: {
            initAllBooks(){
                let _this=this;
                let params={
                    currentPage: this.currentPage,
                    pageSize : this.pageSize
                }
                $.post("/book/all",params,function (data) {

                    console.log(data)
                    _this.books=data.records;
                    _this.currentPage=data.current;
                    _this.pageSize=data.size;
                    //数据总量
                    _this.total=data.total;
                    //总页数
                    _this.totalPages=data.pages;
                })
            }
            ,
            go(pageNum){
                //小于1 或者 大于total 或者 仍获取当前页数据 直接返回
                if(pageNum<1 || pageNum>this.totalPages || pageNum==this.currentPage){
                    console.log(pageNum + " 无效页码值")
                    return;
                }

                this.currentPage = pageNum;
                this.initAllBooks();
            }
            ,
            initBookTypes() {
                //局部变量,便于get方法中调用vue对象
                let _this = this;


                $.get("/booktype/all", function (data) {

                    _this.bookTypes = data;
                })
            }
            ,
            clearAddForm(){
                //打开新增地址表单时,清空表单数据
                let _this = this;

                _this.typeId=-1;
                _this.name='';
                _this.provider='';
                _this.author='';
                _this.price=0.00;
                _this.detail='';
                // _this.imgsrc='';
                _this.storecount=0;
            }
            ,
            saveBook(){

                //js判断



                //收集表单数据

                let _this = this;

                let params = {
                    typeId:_this.typeId,
                    name:_this.name,
                    provider:_this.provider,
                    author:_this.author,
                    price:_this.price,
                    detail:_this.detail,
                    // imgsrc:_this.imgsrc,
                    storecount:_this.storecount
                }

                console.log(params)

                //数据提交后端接口,新增地址
                $.post("/book/add",params,function (data) {

                    //关闭弹窗
                    $("#myModal").modal('hide')

                })

                //刷新页面
                location.href="/";

            }
        }
        ,
        created(){
            this.initAllBooks();
            this.initBookTypes();
        }

    });


</script>


<div th:insert="publicFooter"/>
<script th:src="@{/publicFooter.js}"></script>


</body>
</html>